package com.cloudfun.campusshare.repo;

import com.cloudfun.campusshare.common.constant.DevicePlatform;
import com.cloudfun.campusshare.common.constant.ProductType;
import com.cloudfun.campusshare.entity.DeviceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

public interface DeviceEntityRepo extends
        PagingAndSortingRepository<DeviceEntity, String>,
        JpaSpecificationExecutor<DeviceEntity>, JpaRepository<DeviceEntity, String> {

    List<DeviceEntity> findByAppId(String appId);

    DeviceEntity findByImei(String imei);

    DeviceEntity findByMachineNo(String machineNo);

    DeviceEntity findByQrCode(String qrCode);

    List<DeviceEntity> findAllByProjectId(String projectId);

    List<DeviceEntity> findBySchoolId(String schoolId);

    List<DeviceEntity> findByImeiIn(Collection<String> imeiList);

    DeviceEntity findBySimCard(String simCard);

    DeviceEntity findByIccid(String iccid);

    DeviceEntity findByUserIdAndImei(String userId, String imei);

    DeviceEntity findByIdAndCreatorIdIn(String id, Collection<String> creatorIds);

    DeviceEntity findByImeiAndCreatorIdIn(String imei, Collection<String> creatorIds);

    List<DeviceEntity> findByUserIdAndImeiIn(String userId, Collection<String> imeiList);

    DeviceEntity findByUserIdAndId(String userId, String id);

    List<DeviceEntity> findByUserId(String userId);

    Long countByUserId(String userId);

    Long countAllByAppId(String appId);

    Long countByUserIdAndLastInteractionTimeNotNull(String userId);

    Long countByLastInteractionTimeNotNull();

    Long countByUserIdAndLastInteractionTimeGreaterThanEqual(String userId, String lastInteractionTimeBegin);

    Long countByWashPointId(String washPointId);

    List<DeviceEntity> findByModeSetId(String modeSetId);

    List<DeviceEntity> findAllByIdIn(Collection<String> deviceIds);

    List<DeviceEntity> findAllByWashPointId(String washPointId);

    List<DeviceEntity> findAllByUserIdAndIdIn(String userId, Collection<String> ids);

    List<DeviceEntity> findAllByCreatorIdInAndIdIn(Collection<String> deviceIds, Collection<String> ids);

    List<DeviceEntity> findByWashPointIdAndProductTypeAndOnline(String washPointId, ProductType type, Boolean online);

    List<DeviceEntity> findAllByConcretePositionIdIn(Collection<String> ids);

    List<DeviceEntity> findAllByUserId(String userId);

    List<DeviceEntity> findByOnlineIsNullAndPlatform(DevicePlatform platform);

    List<DeviceEntity> findByPlatform(DevicePlatform platform);

    List<DeviceEntity> findByPlatformAndOnlineAndImeiLike(DevicePlatform platform, boolean online, String imeiKeyword);

    /**
     * 更新设备上线
     * @param imei
     */
    @Transactional
    @Modifying
    @Query(value = "update device set online=true where imei=?1",nativeQuery = true)
    void updateDeviceOnline(String imei);

    @Modifying
    @Query(value = "update device set status=?1 where imei=?2",nativeQuery = true)
    int updateDeviceStatus(String status,String imei);

    // 统计目前有运营商 并且在使用离线的设备
    @Query(value = "select * from device where user_id is not null and is_using=true and online=false",nativeQuery = true)
    List<DeviceEntity> findAllUsingOfflineDevice();
}
